package control;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Aluno;
import model.Curso;
import model.Professor;
import model.Turma;
import dao.DAOAluno;
import dao.DAOCurso;
import dao.DAOProfessor;
import dao.DAOTurma;

/**
 * Servlet implementation class DeletarEntradasServlet
 */
public class DeletarEntradasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeletarEntradasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String chave = request.getParameter("chave");
		String classe = request.getParameter("classe");
		DAOAluno daoaluno = new DAOAluno();
		DAOCurso daocurso = new DAOCurso();
		DAOTurma daoturma = new DAOTurma();
		DAOProfessor daoprofessor = new DAOProfessor();
		
		if (classe.equals("aluno")){			
			daoaluno.begin();
			Aluno a = daoaluno.find(chave);
			List<Turma> tms = a.getTurmas();
			for (int i=0; i < tms.size();){ 
				Turma turma = tms.get(i);
				turma.removeAluno(a);
				daoturma.merge(turma);
			}
			daoaluno.merge(a);
			daoaluno.remove(a);			
			daoaluno.commit();
			
		} else if (classe.equals("curso")){			
			daocurso.begin();
			Curso c = daocurso.find(chave);
			List<Turma> tms = c.getTurmas();
			for (Turma turma : tms) {
				List<Aluno> als = turma.getAlunos();
				for (Aluno aluno : als) {
					aluno.removeTurma(turma);					
				}
				Professor auxp = turma.getProfessor();
				Curso auxc = turma.getCurso();
				turma.setProfessor(null);
				turma.setCurso(null);
				daoprofessor.merge(auxp);
				daocurso.merge(auxc);
				daoturma.merge(turma);
			}
			daocurso.remove(c);
			daocurso.commit();
			
		} else if (classe.equals("turma")){
			int a = Integer.parseInt(chave);			
			daoturma.begin();
			Turma t = daoturma.find(a);
			List<Aluno> als = t.getAlunos();
			for (Aluno aluno : als) {
				try{
					aluno.removeTurma(t);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			Professor auxp = t.getProfessor();
			Curso auxc = t.getCurso();
			t.setProfessor(null);
			t.setCurso(null);
			daoprofessor.merge(auxp);
			daocurso.merge(auxc);
			daoturma.merge(t);
			daoturma.remove(t);
			daoturma.commit();
			
		} else if (classe.equals("prof")){			
			daoprofessor.begin();
			Professor p = daoprofessor.find(chave);
			List<Turma> tms = p.getTurmas();
			for (int i=0; i < tms.size();){ 
				Turma turma = tms.get(i);
				turma.setProfessor(null);				
			}
			daoprofessor.merge(p);
			daoprofessor.remove(p);
			daoprofessor.commit();			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
